<template>
  <el-dialog :title="title" :visible="showVisible" width="30%" @open="handleOpen" @close="handleClose">
    <el-form ref="form" :model="form" label-width="120px" :rules="rules">
      <el-form-item label="岗位名称" prop="postName">
        <el-input v-model="form.postName" />
      </el-form-item>
      <el-form-item label="岗位负责人" prop="userId">
        <el-select v-model="form.userId" placeholder="请选择岗位负责人">
          <el-option v-for="item in userOptions" :key="item.userId" :label="item.nickName" :value="item.userId" />
        </el-select>
      </el-form-item>
      <el-form-item label="岗位编码" prop="postCode">
        <el-input v-model="form.postCode" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio v-model="form.status" :label="false">正常</el-radio>
        <el-radio v-model="form.status" :label="true">停用</el-radio>
      </el-form-item>
      <el-form-item label="备注信息" prop="remark">
        <el-input v-model="form.remark" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" @click="handleClose">取 消</el-button>
            <el-button type="primary" size="mini" @click="submitForm">确 定</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
  import { getUserOptions } from '@/api/user'
  import { insertPost, updatePost } from '@/api/post'
  export default {
    name: 'PostDialog',
    props: {
      title: {
        type: String,
        default: '提示'
      },
      showVisible: {
        type: Boolean,
        default: false
      },
      editData: {
        type: Object,
        default: () => {}
      }
    },
    data() {
      return {
        userOptions: [], // 用户信息选项
        form: {
          postId: '',
          userId: '',
          postName: '',
          postCode: '',
          status: false,
          remark: ''
        },
        rules: {
          userId: [{ required: true, message: '请选择岗位负责人', trigger: 'change' }],
          postName: [
            { required: true, message: '请输入岗位名称', trigger: 'blur' },
            { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
          ],
          postCode: [
            { required: true, message: '请输入岗位编码', trigger: 'blur' },
            { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
          ],
          status: [{ required: true, message: '请选择状态', trigger: 'change' }],
          remark: [{ max: 50, message: '长度在 255 个字符以内', trigger: 'blur' }]
        }
      }
    },
    methods: {
      async getUserOptions() {
        this.userOptions = await getUserOptions()
      },
      handleClose() {
        // 重置表单
        this.$refs.form.resetFields()
        this.$emit('update:show-visible', false)
      },

      submitForm() {
        this.$refs.form.validate(async valid => {
          if (valid) {
            //   console.log('通过检验')
            if (this.form.postId) {
              await updatePost(this.form)
            } else {
              await insertPost(this.form)
            }
            this.handleClose()
            this.$message.success('操作成功')
            this.$emit('refreshData')
          } else {
            return false
          }
        })
      },
      /**
       * 打开弹窗的时候执行
       */
      handleOpen() {
        if (this.title === '新增岗位') {
          this.form = {
            status: false
          }
        } else {
          this.form = {
            postId: this.editData.postId,
            userId: this.editData.userId,
            postName: this.editData.postName,
            postCode: this.editData.postCode,
            status: this.editData.status,
            remark: this.editData.remark
          }
        }
        this.getUserOptions()
      }
    }
  }
</script>

<style></style>
